#include "hdeviceselect.h"

#include <QCheckBox>
#include <QListWidget>
#include <QGroupBox>
#include <QGridLayout>
#include <QPushButton>
#include <QTreeWidget>
#include <QLabel>
#include <QComboBox>
#include <QListWidgetItem>
#include <QIcon>

HDeviceSelect::HDeviceSelect(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout* mainLayout = new QVBoxLayout(this);

    QGroupBox* devListGroup = new QGroupBox(tr("1")); //
    mainLayout->addWidget(devListGroup);
    {
        QVBoxLayout* layout = new QVBoxLayout(devListGroup);
        layout->setMargin(0);
        m_devListWidget = new QListWidget;
        layout->addWidget(m_devListWidget);

        QWidget* ctrlParmWidget = new QWidget;
        layout->addWidget(ctrlParmWidget);

        QHBoxLayout* ctrlParmWidgetLayout = new QHBoxLayout(ctrlParmWidget);
        m_promiscuousMode = new QCheckBox(tr("123"));
        ctrlParmWidgetLayout->addWidget(m_promiscuousMode);
    }


    QGroupBox* filterParamGroup = new QGroupBox(tr("2"));
    mainLayout->addWidget(filterParamGroup);
    {
        QGridLayout* layout = new QGridLayout(filterParamGroup);
        layout->setMargin(0);
        QLabel* protocolLabel = new QLabel(tr("Protocol"));
        m_protocolBox = new QComboBox;
        layout->addWidget(protocolLabel, 0, 0, 1, 1);
        layout->addWidget(m_protocolBox, 0, 1, 1, 2);

        QStringList protocolList;
        protocolList << tr("IP");
        protocolList << tr("TCP");
        protocolList << tr("UDP");
        m_protocolBox->addItems(protocolList);
    }


    QWidget*   ctrlPanelWidget = new QWidget;
    mainLayout->addWidget(ctrlPanelWidget);
    {
        ctrlPanelWidget->setFixedHeight(35);
        QHBoxLayout* layout = new QHBoxLayout(ctrlPanelWidget);
        layout->setMargin(0);
        m_canelBtn = new QPushButton(tr("Cancel"));
        m_confirmBtn = new QPushButton(tr("Confirm"));
        layout->addWidget(m_canelBtn);
        layout->addWidget(m_confirmBtn);

        m_canelBtn->setFixedSize(85, 30);
        m_confirmBtn->setFixedSize(85, 30);
    }
}

void HDeviceSelect::initData(const QVector<DeviceInfo> &devInfo)
{
    QListWidgetItem *item;
    for (const DeviceInfo& info : devInfo) {

        item = new QListWidgetItem(QIcon(":/res/images/corporation.png"), info.strNetDevname, m_devListWidget);
        item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable);
        item->setCheckState(Qt::Unchecked);
        item->setSizeHint(QSize(item->sizeHint().width(), 30));
        //devItemVector.push_back(item);

        item = new QListWidgetItem(info.strNetDevDescribe, m_devListWidget);
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);

        item = new QListWidgetItem(info.strIPV4FamilyName, m_devListWidget);
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);

        item = new QListWidgetItem(info.strIPV4Addr, m_devListWidget);
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);

        item = new QListWidgetItem(info.strIPV6FamilyName, m_devListWidget);
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);

        item = new QListWidgetItem(info.strIPV6Addr, m_devListWidget);
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);

        item = new QListWidgetItem(QString("-----------------------------------------------"
                                           "-----------------------------------------------"), m_devListWidget);
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
        item->setSizeHint(QSize(item->sizeHint().width(), 5));
    }
}

HDeviceSelectDlg::HDeviceSelectDlg(QWidget *parent) :
    QDialog (parent)
{
    m_context = new HDeviceSelect(this);
    QHBoxLayout* mainLayout = new QHBoxLayout(this);

    mainLayout->addWidget(m_context);

    setMinimumSize(550, 450);
}

void HDeviceSelectDlg::initData(const QVector<DeviceInfo> &devInfo)
{
    m_context->initData(devInfo);
}
